<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/21
 * Time: 14:42
 */

namespace ticket\modules\backend\models;


class TicketSearch extends TicketInfo
{
    public function rules()
    {
        return [
            ['name','string'],
            ['type_id','integer'],
            ['ticket_sn','string'],
            ['state','safe'],
            ['category','safe']
        ];
        
    }


    public function search($params)
    {
        if (isset($params['name']) and !empty($params['name'])){
            $params['TicketSearch']['name'] = urldecode($params['name']);
        }else{
            $params['TicketSearch']['name'] = '';
        }
        if (isset($params['ticket_sn']) and !empty($params['ticket_sn'])){
            $params['TicketSearch']['ticket_sn'] = $params['ticket_sn'];
        }else{
            $params['TicketSearch']['ticket_sn'] = '';
        }
        if (isset($params['state']) and !empty($params['state'])){
            $params['TicketSearch']['state'] = $params['state'];
        }else{
            $params['TicketSearch']['state'] = '';
        }
        if (isset($params['category']) and !empty($params['category'])){
            $params['TicketSearch']['category'] = $params['category'];
        }else{
            $params['TicketSearch']['category'] = '';
        }

        $query = TicketInfo::find();
        if ($this->load($params) and $this->validate()){
            if (!empty($params['TicketSearch']['name'])){
                $query->andFilterWhere(['like','name',$this->name]);
            }
            if (!empty($params['TicketSearch']['state'])){
                $query->andFilterWhere(['tk_ticket_info.state' => $this->state]);
            }
            if (!empty($params['TicketSearch']['ticket_sn'])){
                $query->andFilterWhere(['ticket_sn' => $this->ticket_sn]);
            }
            if (!empty($params['TicketSearch']['category'])){
                $query->andFilterWhere(['category' => $this->category]);
            }

            if (!empty($params['OrderSearch']['pay_status'])){
                $query->andFilterWhere(['type_id' => $this->type_id]);
            }

        }

        return $query;
    }
    
    
    
}